package cn.aaalei.controller;

import cn.aaalei.pojo.LoginProfileInfo;
import cn.aaalei.service.ActionService;
import cn.aaalei.service.DataSaveService;
import cn.aaalei.utils.Result;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/action")
@PreAuthorize("hasAnyRole('ADMIN')")
public class ActionController {

    @Autowired
    private DataSaveService dataSaveService;

    @Autowired
    private ActionService actionService;

    @PostMapping("/login")
    public String login(@RequestBody Map<String,String> requestMap) throws Exception {
        return actionService.login(requestMap.get("studentNo"),requestMap.get("password"));
    }

    @PostMapping("/unbind")
    public Result unbind(@RequestBody Map<String,String> requestMap)throws Exception {
        //获取之前积分
        Integer score = dataSaveService.findUserScore(getLoginUser());
        if(score < 10){
            return Result.error("积分不足");
        }
        //解绑
        String res = actionService.unbind(requestMap.get("userId"));
        if(!JSONObject.parseObject(res).getString("code").equals("0")){
            return Result.error("解绑时UserId出现异常...");
        }
        dataSaveService.updateUserScore(getLoginUser(),score - 10);
        Result result = new Result();
        result.put("name",requestMap.get("realName"));
        return result;
    }

    @PostMapping("/evaluate")
    public Result evaluate(@RequestBody Map<String,String> requestMap)throws Exception {
        LoginProfileInfo user = dataSaveService.findProfileInfoByUserId(requestMap.get("userId"));
        String res = actionService.evaluate(user.getStudentNo(),user.getRealName(),requestMap.get("taskName"),requestMap.get("activityId"),requestMap.get("taskId"),requestMap.get("userId"));
        if(!JSONObject.parseObject(res).getString("code").equals("0")){
            return Result.error(JSONObject.parseObject(res).getString("msg"));
        }
        Result result = new Result();
        return result;
    }

    @PostMapping("/modifySign")
    public Result modifySign(@RequestBody Map<String,String> requestMap)throws Exception{
        //获取之前积分
        Integer score = dataSaveService.findUserScore(getLoginUser());
        if(score < 30){
            return Result.error("积分不足");
        }
        //补签
        String res = actionService.modifySignStatus(requestMap.get("userId"),requestMap.get("activityId"),requestMap.get("taskId"), requestMap.get("rollcallId"),requestMap.get("taskName"),requestMap.get("studentNo"),requestMap.get("realName"));
        if(!JSONObject.parseObject(res).getString("code").equals("0")){
            return Result.error(JSONObject.parseObject(res).getString("msg"));
        }
        dataSaveService.updateUserScore(getLoginUser(),score - 30);
        Result result = new Result();
        result.put("name",requestMap.get("realName"));
        return result;
    }

    @PostMapping("/pay")
    public Result pay(@RequestBody Map<String,String> requestMap)throws Exception{
        if(requestMap.get("key").equals("")){
            return Result.error("卡密不能为空");
        }
        //可以为其他账号充值
        int res = actionService.updateUserScore(requestMap.get("username"),requestMap.get("key"));
        if (res == 0){
            return Result.error("请检查充值卡密是否有误");
        }
        //充值积分
        Result result = new Result();
        return result;
    }

    @PostMapping("/autoSign")
    public Result autoSign(@RequestBody Map<String,String> requestMap)throws Exception{
        //获取之前积分
        Integer score = dataSaveService.findUserScore(getLoginUser());
        if(score < 300){
            return Result.error("积分不足");
        }
        //添加自动签到
        Integer res = actionService.addAutoSign(requestMap.get("username"),requestMap.get("password"));
        if (res == 1){
            return Result.error("添加账号时出现异常");
        }
        dataSaveService.updateUserScore(getLoginUser(),score - 300);
        Result result = new Result();
        return result;
    }

    @PostMapping("/getMyActivity")
    public String getMyActivity(@RequestBody Map<String,String> requestMap)throws Exception{
        //获取我的活动
        return actionService.getMyActivity(requestMap.get("userId"));
    }

    @RequestMapping("/dailySign")
    public Result dailySign(){
        //今天已经签过到
        String msg = actionService.dailySign(getLoginUser());
        if(msg == null){
            return Result.error("今天已经签过到啦！");
        }
        Result result = new Result();
        result.put("msg",msg);
        return result;
    }

    private String getLoginUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication.getName();
    }

}
